package com.dzl.enterprise;

import com.dzl.admin.pojo.SysAdmin;
import com.dzl.admin.pojo.SysMenu;
import com.dzl.admin.pojo.SysRole;
import com.dzl.admin.pojo.SysRoleMenu;
import com.dzl.admin.service.SysAdminService;
import com.dzl.admin.service.SysMenuService;
import com.dzl.admin.service.SysRoleMenuService;
import com.dzl.admin.service.SysRoleService;
import com.dzl.base.controller.BaseController;
import com.dzl.constant.CommonConstants;
import com.dzl.constant.WelfareGoodsEnum;
import com.dzl.goods.dao.WelfareGoodsAttrMapper;
import com.dzl.goods.dao.WelfareGoodsClassifyMapper;
import com.dzl.goods.dao.WelfareGoodsMapper;
import com.dzl.goods.pojo.WelfareGoods;
import com.dzl.goods.pojo.WelfareGoodsAttr;
import com.dzl.goods.pojo.WelfareGoodsClassify;
import com.dzl.micro.pojo.WelfareMicroPage;
import com.dzl.micro.service.WelfareMicroPageService;
import com.dzl.util.EmptyUtils;
import com.dzl.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 企业管理初始化权限菜单
 * @author liuguanjun
 * @since 2022-02-16 16:29:41
 */
@Slf4j
@RestController
@RequestMapping("welfareEnterpriseTest")
public class WelfareEnterpriseTestController extends BaseController {

    @Autowired
    private SysAdminService sysAdminService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private WelfareGoodsMapper welfareGoodsMapper;
    @Autowired
    private WelfareGoodsClassifyMapper welfareGoodsClassifyMapper;
    @Autowired
    private WelfareGoodsAttrMapper welfareGoodsAttrMapper;
    @Autowired
    private WelfareMicroPageService welfareMicroPageService;

    /**
     * 初始化企业端菜单角色权限
     * @date 2022-02-16 16:20:00
     * @author liuguanjun
     * @param enterpriseId 企业id
     */
    @GetMapping("syncRoleMenu")
    public JsonResult<Integer> syncRoleMenu(@RequestParam("enterpriseId") Integer enterpriseId) {

        log.info("=====================企业管理初始化权限菜单:删除原来角色和菜单=================");
        SysRole deleteParam = new SysRole();
        deleteParam.setEnterpriseId(enterpriseId);
        sysRoleService.delete(deleteParam);

        //初始化cms和系统的管理权限
        log.info("=====================企业管理初始化权限菜单:新增角色=================");
        SysRole role = new SysRole();
        role.setEnterpriseId(enterpriseId);
        role.setName("超级管理员");
        role.setCreateTime(new Date());
        role.setUpdateTime(new Date());
        role.setCreater(getAdminInfo().getId());
        role.setUpdater(getAdminInfo().getId());
        role.setStatus(CommonConstants.YES);
        sysRoleService.insertSelective(role);

        log.info("=====================企业管理初始化权限菜单:新增菜单并关联角色=================");
        //addDefaultMenuAndRelRole(enterpriseId,role.getId(),getAdminInfo().getId());
        List<SysMenu> menuList = sysMenuService.queryAllMenu();
        //添加菜单角色关联
        ArrayList<SysRoleMenu> roleMenuList = new ArrayList<>();
        menuList.forEach(menu->{
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menu.getId());
            sysRoleMenu.setRoleId(role.getId());
            sysRoleMenu.setCreateTime(new Date());
            sysRoleMenu.setUpdateTime(new Date());
            roleMenuList.add(sysRoleMenu);
        });
        sysRoleMenuService.insertList(roleMenuList);

        log.info("=====================企业管理初始化权限菜单:修改原用户角色id=================");
        SysAdmin sysAdminQuery = new SysAdmin();
        sysAdminQuery.setStatus(CommonConstants.YES);
        sysAdminQuery.setEnterpriseId(enterpriseId);
        List<SysAdmin> sysAdmins = sysAdminService.select(sysAdminQuery);
        if(EmptyUtils.isNotEmpty(sysAdmins)){
            sysAdmins.forEach(x->{
                x.setRoleId(role.getId());
                sysAdminService.updateByPrimaryKeySelective(x);
            });
        }
        return JsonResult.success(1);
    }

    @GetMapping("updateRoleMenu")
    public JsonResult<Integer> updateRoleMenu(@RequestParam("roleId") Integer roleId) {
        SysRoleMenu deleteParam = new SysRoleMenu();
        deleteParam.setRoleId(roleId);
        sysRoleMenuService.delete(deleteParam);
        SysRoleMenu sysRoleMenuQuery = new SysRoleMenu();
        sysRoleMenuQuery.setRoleId(1);
        List<SysRoleMenu> roleMenuList = sysRoleMenuService.select(sysRoleMenuQuery);
        roleMenuList.forEach(x->{
            x.setCreateTime(new Date());
            x.setUpdateTime(new Date());
            x.setId(null);
            x.setRoleId(roleId);
        });
        sysRoleMenuService.insertList(roleMenuList);
        return JsonResult.success(1);
    }

    @GetMapping("syncGoods")
    public JsonResult<Integer> syncGoods(@RequestParam("enterpriseId") Integer enterpriseId) {
        Example exampleGoods=Example.builder(WelfareGoods.class).where(Sqls.custom()
                .andEqualTo("status", CommonConstants.YES)
                .andNotEqualTo("goodsStatus", WelfareGoodsEnum.GOODS_STATUS_TYPE_DRAFT.getCode())
                .andEqualTo("isJicai", CommonConstants.NO)
                .andEqualTo("syncNonSelfSupport", CommonConstants.YES)
                .andEqualTo("enterpriseId",CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID)).build();
        List<WelfareGoods> goodsList = welfareGoodsMapper.selectByExample(exampleGoods);
        if(EmptyUtils.isNotEmpty(goodsList)){
            goodsList.forEach(x->{
                x.setEnterpriseId(enterpriseId);
                //x.setIsShow(CommonConstants.YES);
                x.setCreateTime(new Date());
                x.setUpdateTime(new Date());
                x.setPlatformId(x.getId());
                if(x.getGoodsStatus()==WelfareGoodsEnum.GOODS_STATUS_TYPE_DOWN.getCode()){
                    x.setGoodsStatus(WelfareGoodsEnum.GOODS_STATUS_TYPE_DOWN_PLATFORM.getCode());
                }
                x.setId(null);
            });
            //批量新增企业关联商品
            welfareGoodsMapper.insertList(goodsList);
        }
        //查询平台端的所有商品分类
        Example exampleClassify=Example.builder(WelfareGoodsClassify.class).where(Sqls.custom()
                .andEqualTo("status", CommonConstants.YES)
                .andEqualTo("enterpriseId", 0)).build();
        List<WelfareGoodsClassify> goodsClassifyList = welfareGoodsClassifyMapper.selectByExample(exampleClassify);
        if(EmptyUtils.isNotEmpty(goodsClassifyList)){
            goodsClassifyList.forEach(x->{
                x.setCreateTime(new Date());
                x.setUpdateTime(new Date());
                x.setEnterpriseId(enterpriseId);
                x.setPlatformId(x.getId());
                x.setId(null);
            });
            //批量新增企业关联商品分类
            welfareGoodsClassifyMapper.insertList(goodsClassifyList);
        }
        Example exampleGoodsAttr=Example.builder(WelfareGoodsAttr.class).where(Sqls.custom()
                .andEqualTo("status", CommonConstants.YES)
                .andEqualTo("enterpriseId", CommonConstants.DEFAULT_PLATFORM_ENTERPRISE_ID)).build();
        List<WelfareGoodsAttr> goodsAttrList = welfareGoodsAttrMapper.selectByExample(exampleGoodsAttr);
        if(EmptyUtils.isNotEmpty(goodsAttrList)){
            goodsAttrList.forEach(x->{
                x.setCreateTime(new Date());
                x.setUpdateTime(new Date());
                x.setEnterpriseId(enterpriseId);
                x.setPlatformAttrId(x.getId());
                x.setPlatformGoodsId(x.getGoodsId());
                x.setId(null);
            });
            //批量新增企业关联商品属性
            welfareGoodsAttrMapper.insertList(goodsAttrList);
        }
        //批量修改企业商家商品的三级分类
        welfareGoodsMapper.updateWelfareGoodsClassifyByEnterpriseId(enterpriseId);
        //修改商品分类的父分类
        Example exampleUpdateClassify=Example.builder(WelfareGoodsClassify.class).where(Sqls.custom()
                .andEqualTo("status", CommonConstants.YES)
                .andNotEqualTo("parentId",0)
                .andEqualTo("enterpriseId", enterpriseId)).build();
        List<WelfareGoodsClassify> goodsClassifyList_ = welfareGoodsClassifyMapper.selectByExample(exampleUpdateClassify);
        goodsClassifyList_.forEach(x->{
            WelfareGoodsClassify welfareGoodsClassifyQuery = new WelfareGoodsClassify();
            welfareGoodsClassifyQuery.setEnterpriseId(enterpriseId);
            welfareGoodsClassifyQuery.setPlatformId(x.getParentId());
            WelfareGoodsClassify wgc = welfareGoodsClassifyMapper.selectOne(welfareGoodsClassifyQuery);
            if(EmptyUtils.isNotEmpty(wgc)){
                x.setParentId(wgc.getId());
                welfareGoodsClassifyMapper.updateByPrimaryKeySelective(x);
            }
        });
        //新增微页面首页
        WelfareMicroPage welfareMicroPage = new WelfareMicroPage();
        welfareMicroPage.setEnterpriseId(enterpriseId);
        welfareMicroPage.setStatus(CommonConstants.YES);
        welfareMicroPage.setIsMovie(CommonConstants.NO);
        welfareMicroPage.setIsHome(CommonConstants.YES);
        welfareMicroPage.setName("首页");
        welfareMicroPage.setShowType(2);
        welfareMicroPage.setCreateTime(new Date());
        welfareMicroPage.setCreaterName("首页");
        welfareMicroPageService.insertSelective(welfareMicroPage);
        //新增电影专区微页面
        WelfareMicroPage welfareMicroMoviePage = new WelfareMicroPage();
        welfareMicroMoviePage.setEnterpriseId(enterpriseId);
        welfareMicroMoviePage.setStatus(CommonConstants.YES);
        welfareMicroMoviePage.setIsMovie(CommonConstants.YES);
        welfareMicroMoviePage.setIsHome(CommonConstants.NO);
        welfareMicroMoviePage.setName("电影专区");
        welfareMicroMoviePage.setShowType(2);
        welfareMicroMoviePage.setCreateTime(new Date());
        welfareMicroMoviePage.setCreaterName("系统创建");
        welfareMicroPageService.insertSelective(welfareMicroMoviePage);
        return JsonResult.success(1);
    }

    /*private void addDefaultMenuAndRelRole(Integer enterpriseId,Integer roleId, Integer sysUserId){
        ArrayList<SysMenu> menu1List = new ArrayList<>();
        ArrayList<SysMenu> menu2List = new ArrayList<>();
        menu1List.add(new SysMenu("用户","","/useSupplier","UseSupplier",1,0));
        menu1List.add(new SysMenu("福利","","/welfareCard","WelfareCard",2,0));
        menu1List.add(new SysMenu("商品","","/goods","Goods",3,0));
        menu1List.add(new SysMenu("订单","","/order","Order",4,0));
        menu1List.add(new SysMenu("财务","","/finance","Finance",5,0));
        menu1List.add(new SysMenu("配置","","/authority","Authority",10,0));
        menu1List.forEach(menu->{
            menu.setEnterpriseId(enterpriseId);
            menu.setParentId(0);
            menu.setCreateTime(new Date());
            menu.setUpdateTime(new Date());
            menu.setCreater(sysUserId);
            menu.setUpdater(sysUserId);
            menu.setLevel(1);
            menu.setHidden(CommonConstants.YES);
            menu.setIsEnable(CommonConstants.YES);
            menu.setStatus(CommonConstants.YES);
        });
        sysMenuService.insertList(menu1List);
        menu1List.forEach(menu->{
            if(menu.getSort()==1){
                menu2List.add(new SysMenu("小程序用户","useSupplier/useManagement/index","usemanagement","Usemanagement",1,menu.getId()));
                menu2List.add(new SysMenu("供应商管理","useSupplier/supplieManagement/index","supplieManagement","SupplieManagement",2,menu.getId()));
                menu2List.add(new SysMenu("业务员管理","finance/salesmanAdmin/index","salesmanAdmin","SalesmanAdmin",3,menu.getId()));
                menu2List.add(new SysMenu("客户管理","useSupplier/pointsManage/index","/useSupplier","UseSupplier",4,menu.getId()));
            }else if(menu.getSort()==2){
                menu2List.add(new SysMenu("福利卡管理","welfareCard/welfareCardManagement/index","welfareCardManagement","WelfareCardManagement",1,menu.getId()));
                menu2List.add(new SysMenu("兑换券管理","welfareCard/voucherManagement/index","voucherManagement","VoucherManagement",2,menu.getId()));
                menu2List.add(new SysMenu("福利卡类型","welfareCard/cardManagement/index","cardManagement","CardManagement",3,menu.getId()));
                menu2List.add(new SysMenu("余额转移记录","welfareCard/cardTransfer/index","cardTransfer","CardTransfer",4,menu.getId()));
            }else if(menu.getSort()==3){
                menu2List.add(new SysMenu("常规商品管理","goods/goodsManagement/index","goodsManagement","GoodsManagement",1,menu.getId()));
                menu2List.add(new SysMenu("商品分类管理","goods/goodsClassification/index","goodsClassification","GoodsClassification",2,menu.getId()));
                menu2List.add(new SysMenu("商品标签管理","goods/goodsLabelManage/index","goodsLabelManage","GoodsLabelManage",3,menu.getId()));
                menu2List.add(new SysMenu("商品分区管理","goods/goodsPartition/index","goodsPartition","GoodsPartition",4,menu.getId()));
                menu2List.add(new SysMenu("品牌管理","goods/brand/index","brand","Brand",5,menu.getId()));
            }else if(menu.getSort()==4){
                menu2List.add(new SysMenu("常规订单管理","finance/saleFlow/index","saleFlow","SaleFlow",1,menu.getId()));
                menu2List.add(new SysMenu("电影订单管理","finance/movieOrderFlow/index","movieOrderFlow","MovieOrderFlow",2,menu.getId()));
            }else if(menu.getSort()==5){
                menu2List.add(new SysMenu("常规订单流水","useSupplier/useManagement/index","usemanagement","Usemanagement",1,menu.getId()));
                menu2List.add(new SysMenu("电影订单流水","useSupplier/supplieManagement/index","supplieManagement","SupplieManagement",2,menu.getId()));
            }else if(menu.getSort()==10){
                menu2List.add(new SysMenu("微页面管理","decorate/microPage/index","microPage","MicroPage",1,menu.getId()));
                menu2List.add(new SysMenu("关于我们","decorate/aboutUs/index","aboutUs","AboutUs",2,menu.getId()));
                menu2List.add(new SysMenu("权限管理","authority/menuPermission/index","menuPermission","MenuPermission",3,menu.getId()));
            }
        });
        menu2List.forEach(menu->{
            menu.setEnterpriseId(enterpriseId);
            menu.setCreateTime(new Date());
            menu.setUpdateTime(new Date());
            menu.setCreater(sysUserId);
            menu.setUpdater(sysUserId);
            menu.setLevel(2);
            menu.setHidden(CommonConstants.YES);
            menu.setIsEnable(CommonConstants.YES);
            menu.setStatus(CommonConstants.YES);
        });
        sysMenuService.insertList(menu2List);
        //添加菜单角色关联
        ArrayList<SysRoleMenu> roleMenuList = new ArrayList<>();
        ArrayList<SysMenu> menuList = new ArrayList<>();
        menuList.addAll(menu1List);
        menuList.addAll(menu2List);
        menuList.forEach(menu->{
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setMenuId(menu.getId());
            sysRoleMenu.setRoleId(roleId);
            sysRoleMenu.setCreateTime(new Date());
            sysRoleMenu.setUpdateTime(new Date());
            roleMenuList.add(sysRoleMenu);
        });
        sysRoleMenuService.insertList(roleMenuList);
    }*/


}
